package flower.service;

import flower.domain.Flower;

import java.util.Scanner;

public class FlowerMgr {
    public static Flower[] flowers = new Flower[5];
    //静态代码块，为鲜花对象初始化
    static {
        flowers[0] = new Flower(1001, "金色海岸-香槟玫瑰", "10", 318.0, "2016-12-12", "王二妮", " ");
        flowers[1] = new Flower(1002, "初心永不变-白玫瑰", "99", 538.0, "2016-12-20", "张欣雨", " ");
        flowers[2] = new Flower(1003, "梦里花开-紫色桔梗", "40", 148.0, "2016-12-24", "王二妮", "眼波如水，映出窗外繁星点点");
        flowers[3] = new Flower(1004, "心花怒放-粉色百合", "120", 188.0, "2016-12-26", "王二妮", "内层银灰色厚棉纸，外层深蓝色条纹纸，银灰色缎带");
        flowers[4] = new Flower(1005, "致青春-无声的爱", "1000", 219.0, "2016-12-28", "张欣雨", "我爱你，胜过爱爱情，更胜过爱自己！");
    }
    //主菜单
    public void mainMenu() {
        boolean flag = true;
        while (flag) {
            Scanner sc = new Scanner(System.in);
            System.out.println("================欢迎光临“七彩鲜花“销售管理系统=============================");
            System.out.println("1.查询销售订单");
            System.out.println("2.增加销售订单");
            System.out.println("3.删除销售订单");
            System.out.println("4.修改销售订单");
            System.out.println("5.退出系统");
            System.out.println("=====================================================================");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    findFlower(flowers);
                    break;
                case 2:
                    addFlower(flowers);
                    break;
                case 3:
                    deleteFlower( flowers);
                    break;
                case 4:
                    updateFlower( flowers);
                    break;
                case 5:
                    flag = false;
                    System.out.println("感谢使用“七彩鲜花“销售管理系统，欢迎下次光临！");
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
                    mainMenu();
                    break;
            }
        }
    }
    //查询
    public void findFlower(Flower[] fs) {
        // 打印表头，每个字段指定宽度（可根据实际数据长度调整）
        System.out.printf("%-8s%-20s%-10s%-10s%-10s%-10s%s%n",
                "编号", "鲜花名称", "销售数量", "价格", "销售日期", "销售员", "备注");
        for (int i = 0; i < fs.length; i++) {
            Flower f = fs[i];
            // 按对应宽度打印数据，`-`表示左对齐
            System.out.printf("%-8d%-20s%-10s%-10.1f%-15s%-10s%s%n",
                    f.getId(),
                    f.getName(),
                    f.getSalesNum(),
                    f.getPrice(),
                    f.getSalesDate(),
                    f.getSalesperson(),
                    f.getNote());
        }
    }
    //添加
    public void addFlower(Flower[] fs) {
        // 创建Scanner对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入鲜花名称：");
        String name = sc.next();
        System.out.println("请输入鲜花销售数量：");
        String salesNum = sc.next();
        System.out.println("请输入鲜花价格：");
        double price = sc.nextDouble();
        System.out.println("请输入鲜花销售日期：");
        String salesDate = sc.next();
        System.out.println("请输入鲜花销售员：");
        String salesperson = sc.next();
        System.out.println("请输入鲜花备注：");
        String note = sc.next();
        // 创建Flower对象，为数组扩容
        Flower f = new Flower(flowers[fs.length - 1].getId() + 1, name, salesNum, price, salesDate, salesperson, note);
        Flower[] newFlowers = new Flower[fs.length + 1];
        // 将原数组中的元素复制到新数组中
        for (int i = 0; i < fs.length; i++) {
            newFlowers[i] = fs[i];
        }
        // 将创建的Flower对象添加到新数组中
        newFlowers[fs.length] = f;
        // 将新数组赋给数组变量fs
        flowers = newFlowers;
    }
    //删除
    public void deleteFlower(Flower[] fs) {
        // 创建Scanner对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的鲜花编号：");
        int id = sc.nextInt();
        // 创建一个新数组，用于保存删除后的数据
        Flower[] newFlowers = new Flower[fs.length - 1];
        int index = -1;
        for (int i = 0; i < fs.length; i++) {
            if (fs[i].getId() == id){
                index = i;
                break;
            }
        }
        // 判断是否找到要删除的鲜花
        if (index == -1) {
            System.out.println("您要删除的鲜花编号不存在");
            return;
        }
        // 删除指定位置的鲜花
        for (int i = 0; i < newFlowers.length; i++) {
            if (i < index) {
                newFlowers[i] = fs[i];
            } else {
                newFlowers[i] = fs[i + 1];
            }
        }
        // 将新数组赋给数组变量fs
        flowers = newFlowers;
    }
    // 修改鲜花信息
    public void updateFlower(Flower[] fs) {
        // 创建Scanner对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的鲜花编号：");
        int id = sc.nextInt();
        int index = -1;
        // 查找指定编号的鲜花
        for (int i = 0; i < fs.length; i++){
            if (fs[i].getId() == id){
                index = i;
                break;
            }
        }
        // 判断是否找到要修改的鲜花
        if (index == -1) {
            System.out.println("您要修改的鲜花编号不存在");
            return;
        }
        // 打印要修改的鲜花信息
        System.out.println("您要修改的订单详细信息如下");
        System.out.printf("%-8s%-20s%-10s%-10s%-10s%-10s%s%n",
                "编号", "鲜花名称", "销售数量", "价格", "销售日期", "销售员", "备注");
        System.out.printf("%-8d%-20s%-10s%-10.1f%-15s%-10s%s%n",
                flowers[index].getId(),
                flowers[index].getName(),
                flowers[index].getSalesNum(),
                flowers[index].getPrice(),
                flowers[index].getSalesDate(),
                flowers[index].getSalesperson(),
                flowers[index].getNote());
        // 修改鲜花信息
        System.out.println();
        System.out.println("请输入修改后的鲜花名称：");
        String name = sc.next();
        System.out.println("请输入修改后的鲜花销售数量：");
        String salesNum = sc.next();
        System.out.println("请输入修改后的鲜花价格：");
        double price = sc.nextDouble();
        System.out.println("请输入修改后的鲜花销售日期：");
        String salesDate = sc.next();
        System.out.println("请输入修改后的鲜花销售员：");
        String salesperson = sc.next();
        System.out.println("请输入修改后的鲜花备注：");
        String note = sc.next();
        // 创建修改后的鲜花对象
        fs[index] = new Flower(id, name, salesNum, price, salesDate, salesperson, note);
        System.out.println("修改成功");
    }
}
